package com.my.job.service;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzService {

	@Autowired
	private SchedulerFactoryBean schedulerFactory;


	public boolean updateCronExpression(String triggerGroup, String triggerName, String cronExpression) 
	{

		Scheduler scheduler = schedulerFactory.getScheduler();

		try
		{
			TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);

			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

			// 表达式调度构建器
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

			// 按新的cronExpression表达式重新构建trigger
			trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

			// 按新的trigger重新设置job执行
			scheduler.rescheduleJob(triggerKey, trigger);
			
		}
		catch (Exception e)
		{
			return false;
		}
		
		return true;
	}

	/**
	 * 暂停触发器
	 * 
	 * @param triggerName
	 * @param triggerGroup
	 * @throws Exception
	 */
	public boolean pauseTrigger(String triggerName, String triggerGroup) 
	{

		Scheduler scheduler = schedulerFactory.getScheduler();

		try
		{
			TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
			scheduler.pauseTrigger(triggerKey);// 停止触发器
		}
		catch (Exception e)
		{
			return false;
		}
		
		return true;
	}

	/**
	 * 重启触发器
	 * 
	 * @param triggerName
	 * @param triggerGroup
	 * @throws Exception
	 */
	public boolean resumeTrigger(String triggerName, String triggerGroup) 
	{

		Scheduler scheduler = schedulerFactory.getScheduler();

		try
		{
			TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
			scheduler.resumeTrigger(triggerKey);// 重启触发器
		}
		catch (Exception e)
		{
			return false;
		}
		
		return true;
	}
	
	public boolean deleteJob(String triggerName, String triggerGroup)
	{
		Scheduler scheduler = schedulerFactory.getScheduler();

		try
		{
			TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
			Trigger trigger = scheduler.getTrigger(triggerKey);
			JobKey jobKey = trigger.getJobKey();
			scheduler.deleteJob(jobKey);
		}
		catch (Exception e)
		{
			return false;
		}
		
		return true;
	}

	/**
	 * 暂停job
	 * 
	 * @param jobName
	 * @param jobGroup
	 * @throws Exception
	 */
	public boolean pauseJob(String jobName, String jobGroup) throws Exception 
	{

		Scheduler scheduler = schedulerFactory.getScheduler();

		try
		{
			JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
			scheduler.pauseJob(jobKey);
		}
		catch (Exception e)
		{
			return false;
		}
		
		return true;
	}

	/**
	 * 恢复job
	 * 
	 * @param jobName
	 * @param jobGroup
	 * @throws Exception
	 */
	public boolean resumeJob(String jobName, String jobGroup) 
	{

		Scheduler scheduler = schedulerFactory.getScheduler();

		try
		{
			JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
			scheduler.resumeJob(jobKey);
		}
		catch (Exception e)
		{
			return false;
		}
		
		return true;
	}

	/**
	 * 立即运行job
	 * 
	 * @param triggerName
	 * @param triggerGroup
	 * @throws Exception
	 */
	public boolean runJob(String triggerName, String triggerGroup)
	{

		Scheduler scheduler = schedulerFactory.getScheduler();
		
		try
        {
			TriggerKey triggerKey = new TriggerKey(triggerName, triggerGroup);
			Trigger trigger = scheduler.getTrigger(triggerKey);
			JobKey jobKey = trigger.getJobKey();
			scheduler.triggerJob(jobKey);
        }
        catch (Exception e)
        {
  	        return false;
        }
		
		return true;
	}

}
